﻿using PoemGame.Domain.GameAggregate;
using PoemGame.Domain.PlayerAggregate;
using PoemGame.Domain.Services;

namespace PoemGame.ApplicationService
{
    /// <summary>
    /// 游戏工厂
    /// </summary>
    public class GameFactory : IGameFactory
    {
        private readonly IDomainServiceFactory<ICheckGameConditionService> checkerFactory;

        /// <summary>
        /// 游戏工厂
        /// </summary>
        /// <param name="_checkerFactory"></param>

        public GameFactory(IDomainServiceFactory<ICheckGameConditionService> _checkerFactory)
        {
            checkerFactory = _checkerFactory;
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="player"></param>
        /// <param name="gameType"></param>
        /// <param name="playtype"></param>
        /// <param name="description"></param>
        /// <param name="context"></param>
        /// <param name="gameOptions"></param>
        /// <returns></returns>
        public async Task<GameCreationResult> CreateGame(Player player, GameType gameType, PlayType playtype, string description, string context)
        {
            if (player == null) return new GameCreationResult(null, false, EnumGameCreateFaultReason.PlayerIsNull, "玩家不能为空");
            var srv = checkerFactory.GetService(gameType);
            bool b = await srv.CheckGameCondition(context);
            if (b)
            {
                var game =  new Game(  gameType, playtype, description, context) ;
                game.CreatorJoinGame(player);
                return new GameCreationResult(game, true, EnumGameCreateFaultReason.None, $"{player.UserName}游戏创建成功"); ;
            }
            return new GameCreationResult(null, false, EnumGameCreateFaultReason.WrongParameter, "游戏条件不成立");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameType"></param>
        /// <param name="playtype"></param>
        /// <param name="description"></param>
        /// <param name="context"></param>
        /// <param name="gameOptions"></param>
        /// <returns></returns>
        public async Task<GameCreationResult> CreateGame(GameType gameType, PlayType playtype, string description, string context)
        {
            var srv = checkerFactory.GetService(gameType);
            bool b = await srv.CheckGameCondition(context);
            if (b)
            {
                var game = new Game(gameType, playtype, description, context);
                return new GameCreationResult(game, true, EnumGameCreateFaultReason.None, $"游戏创建成功"); ;
            }
            return new GameCreationResult(null, false, EnumGameCreateFaultReason.WrongParameter, "游戏条件不成立");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="players"></param>
        /// <param name="gameType"></param>
        /// <param name="playtype"></param>
        /// <param name="description"></param>
        /// <param name="context"></param>
        /// <param name="gameOptions"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<GameCreationResult> CreateGame(Player[] players, GameType gameType, PlayType playtype, string description, string context)
        {
            var srv = checkerFactory.GetService(gameType);
            bool b = await srv.CheckGameCondition(context);
            if (b)
            {
                var game = new Game(gameType, playtype, description, context);
                foreach (var player in players)
                {
                    game.PlayerJoinGame(player);
                }
                return new GameCreationResult(game, true, EnumGameCreateFaultReason.None, $"游戏创建成功"); ;
            }
            return new GameCreationResult(null, false, EnumGameCreateFaultReason.WrongParameter, "游戏条件不成立");
        }
    }
}
